package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.ShoppBean;
import com.project.mapper.IShoppMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IShoppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ShoppServiceImpl implements IShoppService {
    @Autowired
    private IShoppMapper mapper;

    @Override
    public List<ShoppBean> findByUser(Integer userId) {
        return mapper.findByUser(userId);
    }

    @Override
    public void add(Integer productId, Integer userId) {
        QueryWrapper<ShoppBean> qw = new QueryWrapper<>();
        qw.eq("fk_productId", productId);
        qw.eq("fk_userId", userId);
        Long count = mapper.selectCount(qw);
        if (count == 0) {
            ShoppBean shopp = new ShoppBean(productId, 1, userId);
            mapper.insert(shopp);
        } else {
            ShoppBean shopp = mapper.selectOne(qw);
            shopp.setCount(shopp.getCount() + 1);
            mapper.updateById(shopp);
        }
    }

    @Override
    public void del(Integer id) {
        mapper.deleteById(id);
    }

    @Override
    public void update(Integer shoppId, Integer num) {
        ShoppBean shopp = mapper.selectById(shoppId);
        shopp.setCount(num);
        mapper.updateById(shopp);
    }
}
